package top.happylaishop.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.happylaishop.shop.dao.ShippingDao;
import top.happylaishop.shop.dto.ShippingDto;
import top.happylaishop.shop.dto.UserDto;
import top.happylaishop.shop.pojo.Shipping;
import top.happylaishop.shop.service.ShippingService;
import top.happylaishop.shop.utils.PageDto;
import top.happylaishop.shop.utils.resp.BaseResponse;
import top.happylaishop.shop.utils.resp.RestResponse;

import java.util.Date;

@Service
public class ShippingServiceImpl implements ShippingService {

    @Autowired
    private ShippingDao shippingDao;

    @Override
    public BaseResponse add(ShippingDto shippingDto, UserDto userDto) {
        Shipping shipping = BeanUtil.copyProperties(shippingDto, Shipping.class);
        shipping.setUserId(userDto.getId());
        shipping.setCreateTime(new Date());
        shipping.setUpdateTime(shipping.getCreateTime());
        shipping.setUpdateUserId(userDto.getId());
        shipping.setCreateUserId(userDto.getId());
        shippingDao.insert(shipping);
        return BaseResponse.success();
    }

    @Override
    public BaseResponse delete(Integer shippingId, UserDto currentUser) {
        Integer count = deleteByIdAndUserId(shippingId, currentUser.getId());
        return count > 0 ? BaseResponse.success() : BaseResponse.failed();
    }

    private Integer deleteByIdAndUserId(Integer id, Integer userId) {
        LambdaQueryWrapper<Shipping> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Shipping::getId, id).eq(Shipping::getUserId, userId);
        return shippingDao.delete(lambdaQueryWrapper);
    }

    @Override
    public BaseResponse update(ShippingDto shippingDto, UserDto userDto) {
        UpdateWrapper<Shipping> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(Shipping::getId, shippingDto.getId())
                .eq(Shipping::getUserId, userDto.getId());
        Shipping shipping = BeanUtil.copyProperties(shippingDto, Shipping.class);
        shipping.setUpdateTime(shipping.getCreateTime());
        shipping.setUpdateUserId(userDto.getId());
        int update = shippingDao.update(shipping, updateWrapper);
        return update > 0 ? BaseResponse.success() : BaseResponse.failed();
    }

    @Override
    public RestResponse<ShippingDto> select(UserDto userDto, Integer id) {
        LambdaQueryWrapper<Shipping> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Shipping::getId, id).eq(Shipping::getUserId, userDto.getId());
        return RestResponse.defaultSucessData(BeanUtil.copyProperties(shippingDao.selectOne(lambdaQueryWrapper), ShippingDto.class));
    }

    @Override
    public RestResponse<PageDto<ShippingDto>> list(PageDto<ShippingDto> shippingDtoPageDto, UserDto userDto) {
        Page<Shipping> page = new Page<>(shippingDtoPageDto.getPage(), shippingDtoPageDto.getSize());
        LambdaQueryWrapper<Shipping> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Shipping::getUserId, userDto.getId());
        Page<Shipping> shippingPage = shippingDao.selectPage(page, lambdaQueryWrapper);
        shippingDtoPageDto.setTotal(shippingPage.getTotal());
        shippingDtoPageDto.setList(BeanUtil.copyToList(shippingPage.getRecords(), ShippingDto.class));
        return RestResponse.defaultSucessData(shippingDtoPageDto);
    }

    @Override
    @Transactional
    public BaseResponse updateDefaultShipping(UserDto userDto, Integer shippingId) {
        cleanDefatulShipping(userDto);
        ShippingDto shipping = new ShippingDto();
        shipping.setDefaultShipping(true);
        shipping.setId(shippingId);
        return update(shipping, userDto);
    }

    private void cleanDefatulShipping(UserDto userDto){
        UpdateWrapper<Shipping> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(Shipping::getUserId, userDto.getId()).eq(Shipping::getDefaultShipping, true);
        Shipping shipping = new Shipping();
        shipping.setDefaultShipping(false);
        shipping.setUpdateTime(shipping.getCreateTime());
        shipping.setUpdateUserId(userDto.getId());
        shippingDao.update(shipping, updateWrapper);
    }
}
